{This unit and the accompanying form (CompForm) handle the user input for }
{adding, changing, or deleting components from data set S. }
{In addition to standard Delphi Units, it uses the Editor routines and }
{the Titrator definition file TitrDefs.}

unit CompEdit;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, ExtCtrls, TitrDefs, Editor;

type
  TCompForm = class(TForm)
    Label1: TLabel;
    NameEdit: TEdit;
    CTypeGroup: TRadioGroup;
    Label2: TLabel;
    Label3: TLabel;
    Label4: TLabel;
    AcceptBtn: TButton;
    DeleteBtn: TButton;
    FreeEdit: TEdit;
    ChargeEdit: TEdit;
    TotalEdit: TEdit;
    HelpBtn: TButton;
    Label5: TLabel;
    StdDevEdit: TEdit;

    procedure FormActivate(Sender: TObject);
    procedure AcceptBtnClick(Sender: TObject);
    procedure DeleteBtnClick(Sender: TObject);
    procedure TotalEditExit(Sender: TObject);
    procedure FreeEditExit(Sender: TObject);
    {procedure StdEditExit(Sender: TObject);  }
    procedure ChargeEditExit(Sender: TObject);
    procedure CTypeGroupExit(Sender: TObject);
    procedure NameEditExit(Sender: TObject);
    procedure HelpBtnClick(Sender: TObject);
    procedure StdDevEditExit(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  CompForm: TCompForm;

{******************************************************************************}
{******************************************************************************}

implementation

uses TitrHelp;

{$R *.dfm}

{Procedure WriteScreen writes the contents of the component being edited }
{ (EdComp) to the screen as readable text }

Procedure WriteScreen;

begin
   With CompForm do begin
      NameEdit.Text := EdComp.Name;
      CTypeGroup.ItemIndex := Integer(EdComp.CType);
      TotalEdit.Text := FloatToStrF(EdComp.TotalX,ffExponent, 4,8);
      FreeEdit.Text := FloatToStrF(EdComp.GuessX, ffFixed, 8, 3);
      If (EdComp.CType = CTotDiss) then
          StdDevEdit.Text := FloatToStrF(EdComp.StdDev, ffExponent, 4,8)
         else
          StdDevEdit.Text := FloatToStrF(EdComp.StdDev, ffFixed, 8, 3);
      ChargeEdit.Text := IntToStr(EdComp.Charge);
   end;
end;                                       {End of WriteScreen}

{Formactivate is called whenever CompForm is activated, and writes approprate }
{information to the form, including changes to button and form captions}

procedure TCompForm.FormActivate(Sender: TObject);
begin
   WriteScreen;
   If (EdMode = Add) then begin
      CompForm.Caption := 'Add New Component';
      DeleteBtn.Caption := 'Cancel';
     end
      else begin
         CompForm.Caption := 'Component Editor';
         DeleteBtn.Caption := 'Delete';
        end;
end;                            {End of TCompForm.FormActivate }

{AcceptBtnClick occurs when user presses the lefthand button labelled Accept.}
{If the component being edited already exists, this indicates that the changes
{       made to EdComp should be written to the selected component}
{If the component is new, this is indicates the new component should be added}

procedure TCompForm.AcceptBtnClick(Sender: TObject);
begin
   If (EdMode = Edit) then
     If (MessageDlg('Change this component?',
            mtConfirmation, [mbOK, mbCancel],0) = mrCancel) then
               EdMode := None;          {not confirmed}

   If (EdMode = Add) then
      If (MessageDlg('Add this component to the data set?',
            mtConfirmation, [mbOK, mbCancel],0) = mrCancel) then
               EdMode := None;          {not confirmed}

   CompForm.Close;
end;                           {End of AcceptBtnClick}

{DeleteBtnClick occurs when user presses the rightmost key. }
{If the component being edited already exists, this is a delete key,
{       and the user is asked to confirm deletion (or no change is made).}
{If the component is new, this is a cancel action key and no change is made}

procedure TCompForm.DeleteBtnClick(Sender: TObject);
begin
   If (EdMode = Edit) then begin        {If this was an existing component }
        Edmode := Delete;               {this is a delete key}
        {Now confirm the deletion..}
        If (MessageDlg('Delete this component?',
            mtConfirmation, [mbOK, mbCancel],0) = mrCancel) then
               EdMode := None;          {not confirmed}
       end
       else EdMode := None;             {Else this was an addition, and key}
                                        {indicates cancelling the addition}

       CompForm.Close;                  {exit this form}
end;                                    {End of DeleteBtnClick}

{***************************************************************************}
{     The following set of procedures respond to user inputs by             }
{     Checking the whether new, valid values have been entered and          }
{     saving valid changes to EdComp.                                       }
{***************************************************************************}

procedure TCompForm.TotalEditExit(Sender: TObject);
begin
    TotalEdit.Text := WriteRealEditExp(TotalEdit.Text, EdComp.TotalX, -100,100);
end;

{This currently edits the log free concentration, but should be the log guess}

procedure TCompForm.FreeEditExit(Sender: TObject);
begin
    FreeEdit.Text := WriteRealEditFix(FreeEdit.Text, EdComp.GuessX, -30,2);
end;

{Charge can vary from +100 to -100}

procedure TCompForm.ChargeEditExit(Sender: TObject);
begin
    ChargeEdit.Text := WriteIntEdit(ChargeEdit.Text, EdComp.Charge, -100, 100);
end;

{Only 2 types- known total and fixed dissolved molarity- are currently allowed}

procedure TCompForm.CTypeGroupExit(Sender: TObject);
begin
    EdComp.CType := CompType(CTypeGroup.ItemIndex);
    If (EdComp.CType = CTotDiss) then begin
       Label3.Caption := 'Guess at Log Free Molarity';
       Label5.Caption := 'Std. Deviation in log units'
      end
      else begin
        Label3.Caption := 'Specify Log Free Molarity';
        Label5.Caption := 'Std. Deviation in Molar units';
       end;
end;

{Any name is currently allowed}

procedure TCompForm.NameEditExit(Sender: TObject);
begin
   EdComp.Name := NameEdit.text;
end;

{    End of the "OnExit" editing routines.                                }


procedure TCompForm.HelpBtnClick(Sender: TObject);
begin
    HelpIndex := 4;
    TitrHelpForm.ShowModal;
end;

{Standard deviations must be positive, maximum value is the absurdly high 10}

procedure TCompForm.StdDevEditExit(Sender: TObject);
begin
   StdDevEdit.Text := WriteRealEdit(StdDevEdit.Text, EdComp.StdDev, 0, 10);
end;

end.                            {End of CompEdit }
